package com.yiren.backstage.communication.utils;

public class MessagesUtil {
    /***/
    public static final int SER_PARAM = 1;

    public static final int REBOOT = 2;   //APP更新准备命令
    public static final int PARAM = 3;    //硬件系统通讯参数
    public static final int UPDATE = 4;   //更新APP命令
    public static final int AUTH = 5;     //获取验签命令
    public static final int START_SEND_DATA = 6;       //设置USB或WIFI模式通讯参数
    public static final int SEND_STOP_DRIVING_DATA = 7;//清除验签数据通讯参数
    public static final int OBD_MODELS = 9; //OBD标定车型通讯参数
    public static final int QIANXUN_DATA_CODE = 10;    //千寻数据发送标识
    public static final int A0 = 11;      //与模块建立升级连接
    public static final int A1 = 12;      //向模块发送固件长度
    public static final int A2 = 13;      //向模块发送一帧数据

    private byte[] buf;
    private int cmd;

    public int getCmd() {
        return cmd;
    }

    public void setCmd(int cmd) {
        this.cmd = cmd;
    }

    public MessagesUtil(int cmd, int type, byte[] data, byte[] rawData) {
        this.cmd = cmd;
        int lenth = 20 + data.length + rawData.length;
        buf = new byte[lenth];
        Msg_head msg_head = new Msg_head(cmd, type, lenth, data.length, rawData.length);
        Msg_Body_Raw msg_body_raw = new Msg_Body_Raw(data, rawData);

        System.arraycopy(msg_head.getHead(), 0, buf, 0, msg_head.getHead().length);
        System.arraycopy(msg_body_raw.getBody(), 0, buf, msg_head.getHead().length, msg_body_raw.getBody().length);
    }

    public byte[] getMessage() {
        return this.buf;
    }

    public static void main(String args[]) {
        //new Message
    }

    public static byte[] toLH(int n) {
        byte[] b = new byte[4];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        b[2] = (byte) (n >> 16 & 0xff);
        b[3] = (byte) (n >> 24 & 0xff);
        return b;
    }

    class Msg_head {
        private byte[] head = new byte[4 + 4 + 4 + 4 + 4];
        public int cmd = 0;
        //类型�?0代表�?�? 1代表结束
        public int type = 0;
        public int len = 0;
        public int datalen = 0;
        public int rawDatalen = 0;

        /**
         * @param cmd  命令
         * @param type 消息类型
         * @param len  消息总长�?
         */
        public Msg_head(int cmd, int type, int len, int datalen, int rawDatalen) {
            this.cmd = cmd;
            this.type = type;
            this.len = len;
            this.datalen = datalen;
            this.rawDatalen = rawDatalen;
            byte[] temp = toLH(cmd);
            System.arraycopy(temp, 0, head, 0, temp.length);

            temp = toLH(type);
            System.arraycopy(temp, 0, head, 4, temp.length);

            temp = toLH(len);
            System.arraycopy(temp, 0, head, 8, temp.length);

            temp = toLH(datalen);
            System.arraycopy(temp, 0, head, 12, temp.length);
            temp = toLH(rawDatalen);
            System.arraycopy(temp, 0, head, 16, temp.length);
        }

        /**
         * 将低字节在前转为int，高字节在后的byte数组
         */
        private int vtolh(byte[] bArr) {
            int n = 0;
            for (int i = 0; i < bArr.length && i < 4; i++) {
                int left = i * 8;
                n += (bArr[i] << left);
            }
            return n;
        }

        /**
         * 返回要发送的数组
         */
        public byte[] getHead() {
            return head;
        }
    }


    class Msg_Body_Raw {

        private byte[] body;
        private byte[] data;
        private byte[] rawData;

        public Msg_Body_Raw(byte[] data, byte[] rawData) {
            this.data = data;
            this.rawData = rawData;
            body = new byte[data.length + rawData.length];
            System.arraycopy(data, 0, body, 0, data.length);
            System.arraycopy(rawData, 0, body, data.length, rawData.length);
        }

        public byte[] getData() {
            return data;
        }

        public byte[] rawData() {
            return rawData;
        }

        /**
         * 返回要发送的数组
         */
        public byte[] getBody() {
            return body;
        }
    }
}





